package cn.acmsmu.shiro.test.service;

import org.apache.shiro.authz.AuthorizationException;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.acmsmu.mgsky1.shiro.iface.UserAuthExtAdapter;
import cn.acmsmu.mgsky1.shiro.model.BaseUserModel;
import cn.acmsmu.shiro.test.entity.SysUser;
import cn.acmsmu.shiro.test.mapper.SysRoleMapper;
import cn.acmsmu.shiro.test.mapper.SysUserMapper;
import cn.hutool.core.collection.CollUtil;

/**
 * @Desc:
 * @Author: huangzhiyuan
 * @CreateDate: 2023/8/20 09:54
 * @Modify:
 */
@Service
public class UserAuthExtPointService extends UserAuthExtAdapter {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public BaseUserModel getUserBaseInfoByUserName(String userName) {
        SysUser user = sysUserMapper.findUserByUserName(userName);
        BaseUserModel baseUserModel = new BaseUserModel();
        baseUserModel.setUserId(user.getId());
        baseUserModel.setUserName(user.getUserName());
        baseUserModel.setPassword(user.getPassword());
        return baseUserModel;
    }

    @Override
    public List<String> getUserPermissionsByUserId(Long userId) {
        List<Integer> userRoles = sysRoleMapper.findRoleIdsByUserId(userId);
        if (CollUtil.isEmpty(userRoles)) {
            throw new AuthorizationException();
        }
        List<String> userPermissions = sysRoleMapper.findPermissionsByRoleId(userRoles);
        return userPermissions;
    }
}
